package com.yonyou.jrmob.contacts.contact.activity;

/**
 * Created by lzq on 2016/10/27.
 */
import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;
import android.widget.Toast;

import com.yonyou.jrmob.contacts.R;
import com.yonyou.jrmob.contacts.contact.adapter.UsefulTelAdapter_ct;
import com.yonyou.jrmob.contacts.contact.bean.IndexDetail_ct;
import com.yonyou.uap.um.service.ServiceCallback;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import cn.bingoogolapple.titlebar.BGATitleBar;
import jrmob.yonyou.com.commonlib.app_config_tool.constant.Constant;
import jrmob.yonyou.com.commonlib.app_config_tool.gkh.MAServices_gkh;
import jrmob.yonyou.com.commonlib.app_config_tool.tool.base.CommonUtil;
import jrmob.yonyou.com.commonlib.app_config_tool.tool.base.LogUtils;
import jrmob.yonyou.com.commonlib.app_config_tool.tool.base.StatusBarUtil;
import jrmob.yonyou.com.commonlib.baseui.base.BaseActivityMob_lib;


/**
 * Created by Administrator on 2016/7/12.
 */
public class UsefulTelephoneActivity_ct extends BaseActivityMob_lib {
    private ListView usefulTelListView;
    private List<String> index;
    private List<IndexDetail_ct> indexDetailCts;
    private UsefulTelAdapter_ct adapter;
    private BGATitleBar titleBar;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initActionbar();
        setContentView(R.layout.useful_tel_layout_ct);
        initView(); // 初始化控件
        initListener(); // 添加监听
        initData(); // 默认初始数据
        StatusBarUtil.StatusBarLightMode(this);
    }

    @Override
    public void initView() {
        // set the TextView's link mask, auto link all.
        //useful_detail.setAutoLinkMask(Linkify.ALL);
        titleBar = (BGATitleBar) findViewById(R.id.useful_title_bar);
        usefulTelListView = (ListView) findViewById(R.id.useful_tel);
    }

    public void initListener() {
        titleBar.setDelegate(new BGATitleBar.Delegate() {
            @Override
            public void onClickLeftCtv() {
                finish();
            }

            @Override
            public void onClickTitleCtv() {

            }

            @Override
            public void onClickRightCtv() {

            }

            @Override
            public void onClickRightSecondaryCtv() {

            }
        });
    }

    public void initData() {
        titleBar.setTitleText(getIntent().getStringExtra("title"));
        callService();
    }

    /***
     * 调用后台服务
     */
    private void callService() {

        JSONObject json = new JSONObject();
        try {
            json.put("transtype", Constant.TRANSTYPE_USEFUL_TEL);
            json.put("tenant_id", getUserInfoLRY().getTenant_id());
            json.put("uuidToken",Constant.TOKEN);
            if (!CommonUtil.isNetworkConnected(this)) {
                CommonUtil.showToast(this, "当前网络不可用,请检查!");
                return;
            }
            MAServices_gkh.callMACommonService(this, new ServiceCallback() {
                @Override
                public void execute(JSONObject json) {
                    LogUtils.e("常用电话数据：" + json.toString());
                    //Toast.makeText(MyDepartmentActivity_ct.this, json+"", Toast.LENGTH_LONG).show();
                    JSONObject jsObject;
                    JSONArray jsArray;
                    JSONArray jsArrayCell;
                    try {
                        jsObject = (JSONObject) json.opt("result");
                        Object obj = jsObject.opt("retflag");
                        // 有错误的时候
                        if (null != obj && obj.toString().trim().equals("1")) {
                            String msg = jsObject.optString("msg").toString().trim();
                            showMessage(msg);
                            if (msg.contains("请重新登录")) {
                                turnToLoginActivity(UsefulTelephoneActivity_ct.this);
                            }
                            return;
                        }
                        jsArray = (JSONArray) jsObject.get("CommonContact");
                        index = new ArrayList<String>();
                        indexDetailCts = new ArrayList<IndexDetail_ct>();
                        for (int i = 0; i < jsArray.length(); i++) {
                            index.add(jsArray.getJSONObject(i).getString("index"));
                            jsArrayCell = jsArray.getJSONObject(i).getJSONArray("CellDatas");
                            for (int j = 0; j < jsArrayCell.length(); j++) {
                                IndexDetail_ct indexDetailCt = new IndexDetail_ct();
                                indexDetailCt.setIndex(index.get(i));
                                indexDetailCt.setDetail(jsArrayCell.getJSONObject(j).getString("detail"));
                                indexDetailCt.setTitle(jsArrayCell.getJSONObject(j).getString("title"));
                                indexDetailCts.add(indexDetailCt);
                            }
                        }
                        adapter = new UsefulTelAdapter_ct(UsefulTelephoneActivity_ct.this, indexDetailCts);
                        usefulTelListView.setAdapter(adapter);
                    } catch (JSONException e1) {
                        e1.printStackTrace();
                    }
                }

                @Override
                public void error(String arg0, String arg1) {
                    // 设置登录按钮可用性(登录请求后台有反应后可用)
                    Toast.makeText(UsefulTelephoneActivity_ct.this, arg0 + arg1,
                            Toast.LENGTH_LONG).show();
                }
            }, json,MAServices_gkh.initMaRequestUrl());

        } catch (Exception e) {
            // 设置登录按钮可用性(登录请求后台有反应后可用)
            Log.e("mmmm", e + "");
            e.printStackTrace();
        }
    }

}

